package com.blinddate.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blinddate.context.BaseContext;
import com.blinddate.mapper.UserMapper;
import com.blinddate.pojo.entity.User;
import com.blinddate.properties.WeChatProperties;
import com.blinddate.service.UserService;
import com.blinddate.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j

public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    public static final String WX_LOGIN="https://api.weixin.qq.com/sns/jscode2session";
    @Autowired
    private WeChatProperties weChatProperties;
    @Autowired
    private UserMapper userMapper;

    public User wxlogin(String code) {
        //使用微信接口服务来获取小程序的openid
        String openid=getOpenid(code);
        log.info("{}",openid);
        //openid为空 登录失败 抛出异常
        if(openid==null){
            return null;
        }
        //判断当前用户是否为新用户
        LambdaQueryWrapper<User> qw=new LambdaQueryWrapper<>();
        qw.eq(User::getOpenId,openid);
        User user=userMapper.selectOne(qw);
        log.info("{}",user);
        //如果是新用户 自动完成注册
        if(user==null){
            user=User.builder()
                    .openId(openid)
                    .count(0)
                    .identity(0)
                    .build();
            userMapper.insert(user);
        }
        return user;
        //返回用户
    }
    private String getOpenid(String code){
        Map<String,String> map= new HashMap<>();
        map.put("appid",weChatProperties.getAppid());
        map.put("secret",weChatProperties.getSecret());
        map.put("js_code",code);
        map.put("grant_type","authorization_code");
        String json= HttpClientUtil.doGet(WX_LOGIN,map);
        JSONObject jsonObject= JSON.parseObject(json);
        return jsonObject.getString("openid");
    }

    public User getUser(Long userid) {
        User user=userMapper.selectById(userid);
        return user;
    }

    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    public Boolean identityDecide() {//权限判断  用于调用管理员接口
        Long userId = BaseContext.getCurrentId();
        User user = this.getById(userId);
        if (user.getIdentity()==1||user.getIdentity()==2){
            return true;
        }
        return false;
    }

    @Override
    public Boolean superAdmin() {
        Long userId = BaseContext.getCurrentId();
        User user = this.getById(userId);
        if (user.getIdentity()==2){
            return true;
        }
        return false;
    }

    @Override
    public List<User> getUserList(Integer page, Integer size,Integer book) {
        IPage iPage=new Page(page,size);
        switch (book) {
            /**
             * 已展示的用户（未过期的）
             */
            case 6:
                LambdaQueryWrapper<User> law6 = new LambdaQueryWrapper<>();
                law6.gt(User::getShowEndTime, LocalDateTime.now()).eq(User::getShowStatus,1);
                law6.orderByAsc(User::getShowEndTime);
                List<User> userList6 = userMapper.selectPage(iPage, law6).getRecords();
                return userList6;

            /**
             * 已下架（过期的）
             */
            case 5:
                LambdaQueryWrapper<User> law5 = new LambdaQueryWrapper<>();
                law5.lt(User::getShowEndTime, LocalDateTime.now());
                law5.eq(User::getShowStatus,0);
                law5.orderByAsc(User::getShowEndTime);
                List<User> userList5 = userMapper.selectPage(iPage, law5).getRecords();
                return userList5;
            /**
             * 待展示的user
             */
            case 3:
                LambdaQueryWrapper<User> law = new LambdaQueryWrapper<>();
                law.gt(User::getShowEndTime, LocalDateTime.now());
                law.orderByAsc(User::getShowStatus);
                law.orderByAsc(User::getShowEndTime);
                List<User> userList = userMapper.selectPage(iPage, law).getRecords();
                return userList;
            /**
             * 待下架的user（过期的）
             */
            case 4:
                LambdaQueryWrapper<User> law1 = new LambdaQueryWrapper<>();
                law1.lt(User::getShowEndTime, LocalDateTime.now());
//               law1.eq(User::getShowStatus,1);
                law1.orderByDesc(User::getShowStatus);
                law1.orderByAsc(User::getShowEndTime);
                List<User> userList1 = userMapper.selectPage(iPage, law1).getRecords();
                return userList1;
            case 2:
                LambdaQueryWrapper<User> law2 = new LambdaQueryWrapper<>();
                law2.gt(User::getVipTimeEnd, LocalDateTime.now());
                law2.orderByAsc(User::getVipTimeEnd);
                List<User> userList2 = userMapper.selectPage(iPage, law2).getRecords();
                return userList2;
            case 1:
                LambdaQueryWrapper<User> law3 = new LambdaQueryWrapper<>();
                law3.gt(User::getGreenEndTime, LocalDateTime.now());
                law3.orderByAsc(User::getGreenEndTime);
                List<User> userList3 = userMapper.selectPage(iPage, law3).getRecords();
                return userList3;

        }
        return null;
    }


}
